package com.project.biz.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsSupplierBiz;
import com.project.common.exception.ServiceException;
import com.project.entity.CmsSupplier;
import com.project.mapper.CmsSupplierMapper;
import com.project.pojo.dto.SupplierDTO;
import com.project.pojo.model.AddSupplierModel;
import com.project.pojo.model.SelectSupplierModel;
import com.project.pojo.model.UpdateSupplierModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 供应商业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/12/15 11:08
 * @Version: 1.0
 */
@Service
public class CmsSupplierBizImpl extends ServiceImpl<CmsSupplierMapper, CmsSupplier> implements CmsSupplierBiz {

    /**
     * 分页查询供应商
     * @param selectSupplierModel
     * @return
     */
    public IPage<SupplierDTO> selectSupplier(SelectSupplierModel selectSupplierModel){
        Page<SupplierDTO> page = new Page<>(selectSupplierModel.getPageNo(), selectSupplierModel.getPageSize());
        IPage<SupplierDTO> iPage = this.baseMapper.selectSupplier(page, selectSupplierModel);
        return iPage;
    }

    /**
     * 查询供应商返回集合
     * @param selectSupplierModel
     * @return
     */
    public List<SupplierDTO> selectSupplierList(SelectSupplierModel selectSupplierModel){
        return this.baseMapper.selectSupplier(selectSupplierModel);
    }

    /**
     * 添加供应商
     * @param addSupplierModel
     * @param userId
     * @return
     */
    public int addSupplier(AddSupplierModel addSupplierModel, Long userId){
        CmsSupplier supplier = new CmsSupplier();

        Date nowTime = new Date();
        BeanUtils.copyProperties(addSupplierModel, supplier);
        supplier.setCreateTime(nowTime);
        supplier.setCreateUser(userId);
        supplier.setUpdateTime(nowTime);
        supplier.setUpdateUser(userId);
        supplier.setStatus(1);

        return this.baseMapper.insert(supplier);
    }

    /**
     * 修改供应商
     * @param updateSupplierModel
     * @param userId
     * @return
     */
    public int updateSupplier(UpdateSupplierModel updateSupplierModel, Long userId){
        CmsSupplier supplier = this.baseMapper.selectById(updateSupplierModel.getSupplierId());
        if (null == supplier){
            throw new ServiceException("请选择要修改的供应商");
        }

        Date nowTime = new Date();
        BeanUtils.copyProperties(updateSupplierModel, supplier);
        supplier.setUpdateTime(nowTime);
        supplier.setUpdateUser(userId);
        supplier.setStatus(1);

        return this.baseMapper.updateById(supplier);
    }

    /**
     * 删除供应商
     * @param supplierId 供应商ID
     * @param userId
     * @return
     */
    public int delSupplier(Long supplierId, Long userId){
        CmsSupplier supplier = this.baseMapper.selectById(supplierId);
        if (null == supplier){
            throw new ServiceException("请选择要删除的供应商");
        }
        Date nowTime = new Date();
        supplier.setUpdateTime(nowTime);
        supplier.setUpdateUser(userId);
        supplier.setStatus(-1);

        return this.baseMapper.updateById(supplier);
    }

}
